package top.huhuiyu.springboot3.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import top.huhuiyu.springboot3.util.IpUtils;

import java.util.Optional;

@Component
@Slf4j
public class AppListener implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("应用程序启动成功");
        Integer port = event.getApplicationContext().getEnvironment().getProperty("server.port", Integer.class);
        String contextPath = event.getApplicationContext().getEnvironment().getProperty("server.servlet.context-path", String.class);
        // contextPath = contextPath == null ? "" : contextPath;
        contextPath = Optional.ofNullable(contextPath).orElse("");
        String ip = IpUtils.getLocalIp();
        log.info("=============================================");
        log.debug("http://{}:{}/{}", ip, port, contextPath);
        log.debug("http://{}:{}/{}", "127.0.0.1", port, contextPath);
        log.info("=============================================");
    }
}
